package com.sxfq.loansupermarket.backsystem.modular.refunds.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.refunds.dao.RechargeDao;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.RechargeInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.RefundInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.SearchRechargeInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.service.IRechargeService;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RechargeServiceImpl implements IRechargeService {

    @Autowired
    private RechargeDao dao;

    @Autowired
    ICustomerService customerService;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<RechargeInfo> selectPageListRechargeInfo(Page<RechargeInfo> page, SearchRechargeInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getXcustomerStatus())) {
            params.put("xcustomerStatus", searchInfo.getXcustomerStatus());
        }
        if(!"请选择".equals(searchInfo.getOneChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if(!"请选择".equals(searchInfo.getTwoChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if(!"请选择".equals(searchInfo.getThrChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if ("1".equals(userDto.getRoleid())) {
            params.put("entryClerk", "");
        } else {
            params.put("entryClerk", ShiroKit.getUser().getAccount());
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        String tableName2 = tableHead + "recharge_info";
        params.put("tableName", this.getTableName());
        params.put("tableName1", tableName1);
        params.put("tableName2", tableName2);
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPageRechargeInfo(page, params);
    }

    public int getQueryPagesCount(Page<RechargeInfo> page, SearchRechargeInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getXcustomerStatus())) {
            params.put("xcustomerStatus", searchInfo.getXcustomerStatus());
        }
        if(!"请选择".equals(searchInfo.getOneChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if(!"请选择".equals(searchInfo.getTwoChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if(!"请选择".equals(searchInfo.getThrChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if ("1".equals(userDto.getRoleid())) {
            params.put("entryClerk", "");
        } else {
            params.put("entryClerk", ShiroKit.getUser().getAccount());
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        String tableName2 = tableHead + "recharge_info";
        params.put("tableName", this.getTableName());
        params.put("tableName1", tableName1);
        params.put("tableName2", tableName2);
        return dao.getQueryPagesCount(page, params);
    }

    @Transactional
    public void delete(String id) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "recharge_info";
        String selectSql="select * from "+tableName1+" where id = '" + id + "'";
        RechargeInfo rechargeInfo=sqlMapper.selectOne(selectSql,RechargeInfo.class);
        String updateSql="update "+tableName+" a set a.actual_account_balance=actual_account_balance-'"+rechargeInfo.getActualAccountBalance()+"'," +
                " a.give_account_balance=give_account_balance-'"+rechargeInfo.getGiveAccountBalance()+"', " +
                " a.account_arrears=account_arrears-'"+rechargeInfo.getAccountArrears()+"' where reservations_id='"+rechargeInfo.getReservationsId()+"'";
        String sql = "delete from "+tableName1+" where id = '" + id + "'";
        if("是".equals(rechargeInfo.getIsRecord())){
            sqlMapper.update(updateSql);
        }
        sqlMapper.delete(sql);
    }

    @Transactional
    public void updateGetTime(String reservationsId,String getTime,String time) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        String updatesqltime = "update " + tableName1 + " set get_time='"+time+"' " +
                " where reservations_id='" + reservationsId + "' and get_time='" + getTime + "'";
        sqlMapper.update(updatesqltime);
    }

    public void deletes(String id) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "deal_info";
        String selectSql = "select * from " + tableName + " where id = '" + id + "'";
        RechargeInfo rechargeInfo = sqlMapper.selectOne(selectSql, RechargeInfo.class);
        String deleteSql = "delete from " + tableName + " where customer_id='" + rechargeInfo.getCustomerId() + "' " +
                "and get_time='" + rechargeInfo.getGetTime() + "'";
        sqlMapper.delete(deleteSql);
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }
}

